﻿using MeterialManager.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterialManager.Model
{
    public class Category
    {
        public int CategoryId { get; set; }
        public string CategoryCode { get; set; }
        public string CategoryName { get; set; }


        public static DataTable GetDtCategory()
        {
            DataTable dt = SqlHelper.ExecuteTable("select CategoryId,CategoryCode,CategoryName from Categorys");
            return dt;
        }

        public static List<Category> GetListCategory()
        {
            List<Category> categories = new List<Category>();
            DataTable dt = GetDtCategory();
            foreach (DataRow dr in dt.Rows)
            {
                categories.Add(dr.DataRowToModel<Category>());
            }
            return categories;
        }

        public static int Insert(Category category)
        {
            int rows = SqlHelper.ExecuteNonQuery("INSERT INTO Categorys(CategoryCode,CategoryName) VALUES (@CategoryCode,@CategoryName)",
                new SqlParameter("@CategoryCode",category.CategoryCode),
                new SqlParameter("@CategoryName",category.CategoryName)
                );
            return rows;
        }

        public static int Update(Category category)
        {
            int rows = SqlHelper.ExecuteNonQuery("UPDATE Categorys SET CategoryCode=@CategoryCode,CategoryName=@CategoryName WHERE CategoryId=@CategoryId",
                new SqlParameter("@CategoryId", category.CategoryId),
                new SqlParameter("@CategoryCode", category.CategoryCode),
                new SqlParameter("@CategoryName", category.CategoryName)
                );
            return rows;
        }

        public static int Delete(int categoryId)
        {
            int rows = SqlHelper.ExecuteNonQuery("DELETE FROM Categorys WHERE CategoryId=@CategoryId",
                new SqlParameter("@CategoryId", categoryId)
                );
            return rows;
        }
    }
}
